/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.screen.testscreen.producteditor;

import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.editor.DkPreFlushVisitor;
import com.daikit.daikit4gxt.client.editor.DkSimpleBeanEditorDriver;
import com.daikit.daikit4gxt.client.log.DkLogger;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.editor.ProductEditor;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.google.gwt.core.client.GWT;


/**
 * Controller for Product Editor
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class ProductEditorController
{

	@SuppressWarnings("unused")
	private final DkLogger log = DkLogger.getLog(ProductEditorController.class);

	private ProductData productToBeEdited;
	private String uniqueEditionId;
	private final ProductEditorUI productEditorUI;
	private boolean tab4Visible = true;

	private final ProductEditor productEditor;

	interface ProductDataEditorDriver extends DkSimpleBeanEditorDriver<ProductData, ProductEditor>
	{
		// Nothing
	}

	private final ProductDataEditorDriver productDataEditorDriver = GWT.create(ProductDataEditorDriver.class);

	/**
	 * Constructor
	 * 
	 * @param productEditorUI
	 */
	public ProductEditorController(final ProductEditorUI productEditorUI)
	{
		this.productEditorUI = productEditorUI;
		this.productEditor = new ProductEditor();
		this.productDataEditorDriver.initialize(productEditor);
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// UTILS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @return the currently edited {@link ProductData}
	 */
	public ProductData getEditedModel()
	{
		return getEditor().getEditedModel();
	}

	/**
	 * 
	 * @return whether a product is currently being edited
	 */
	public boolean isEditing()
	{
		return getEditedModel() != null;
	}

	protected abstract void onProductChanged(final ProductData productData);

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// CALLBACKS FROM UI
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @param product
	 *           the {@link ProductData} to be edited
	 */
	public void doEdit(final ProductData product)
	{
		productDataEditorDriver.edit(product);
		Main.controller().invalidateUi(false);
	}

	/**
	 * Do Save currently edited product
	 */
	public void doSave()
	{
		productDataEditorDriver.accept(new DkPreFlushVisitor(true));
		final ProductData flushed = productDataEditorDriver.flush();
		final DkBaseAction<ProductData> saveAction = new DkBaseAction<ProductData>(Main.gwtSampleI18n().action_saving_product(), true)
		{
			@Override
			protected void run()
			{
				Main.controller().rpcProduct().saveProduct(flushed, new DkBaseAsyncCallback<ProductData>(this, true)
				{
					@Override
					public void processSuccess(final ProductData result)
					{
						onProductChanged(result);
						productDataEditorDriver.edit(result);
					}
				});
			}
		};
		saveAction.execute();
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// GETTERS / SETTERS
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	/**
	 * @param productToBeEdited
	 *           the productToBeEdited to set
	 */
	public void setProductToBeEdited(final ProductData productToBeEdited)
	{
		this.productToBeEdited = productToBeEdited;
	}

	/**
	 * @return the productToBeEdited
	 */
	public ProductData getProductToBeEdited()
	{
		return productToBeEdited;
	}

	/**
	 * @return the uniqueEditionId
	 */
	public String getUniqueEditionId()
	{
		return uniqueEditionId;
	}

	/**
	 * @return the productEditorUI
	 */
	public ProductEditorUI getProductEditorUI()
	{
		return productEditorUI;
	}

	/**
	 * @return the productEditor
	 */
	public ProductEditor getEditor()
	{
		return productEditor;
	}

	/**
	 * @return the productDataEditorDriver
	 */
	public ProductDataEditorDriver getEditorDriver()
	{
		return productDataEditorDriver;
	}

	/**
	 * @return the tab4Visible
	 */
	public boolean isTab4Visible()
	{
		return tab4Visible;
	}

	/**
	 * @param tab4Visible
	 *           the tab4Visible to set
	 */
	public void setTab4Visible(final boolean tab4Visible)
	{
		this.tab4Visible = tab4Visible;
	}
}
